package bzapps.weynak;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.google.android.c2dm.C2DMBaseReceiver;
import com.google.android.c2dm.C2DMessaging;

public class C2DMReceiver extends C2DMBaseReceiver {
	
	private Vibrator vibrator;
	private SharedPreferences pref;
	public static String HOST = "http://www.bz-apps.com/weynak";
	
	
	public C2DMReceiver() {
		super("christophe.zoghbi@gmail.com");
	}

	@Override
	public void onRegistered(Context context, String registrationId) {
		
		Log.w("C2DMReceiver-onRegistered", "Sending request to Server");
		pref = PreferenceManager.getDefaultSharedPreferences(context);
		String name = pref.getString("FirstName", "Unknown") + " " + pref.getString("LastName", "Unknown");
	 	String uid = pref.getString("uid", "0"); 
	 	
		try {
			boolean result = ClientOperations.RegisterPush(uid, name, registrationId);
			if(result){
				pref.edit().putBoolean("isPushRegistered", true).commit();
			}

		} catch (Exception e) {
			Log.e("WEYNAK.onRegistered", e.getMessage());
		}
		
		Log.w("C2DMReceiver-onRegistered", registrationId);
	}

	@Override
	public void onUnregistered(Context context) {
		Log.w("C2DMReceiver-onUnregistered", "got here!");
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.w("C2DMReceiver-onError", errorId);
	}

	@Override
	protected void onMessage(Context contxt, Intent intent) {
		Log.w("C2DMReceiver", intent.getStringExtra("payload"));
		Toast.makeText(contxt, "Push Notification", 10).show();
		vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		int icon = R.drawable.weynak_icon; // icon from resources
		CharSequence tickerText = "Weynak notification"; // ticker-text
		long when = System.currentTimeMillis(); // notification time
		Context context = getApplicationContext(); // application Context
		CharSequence contentTitle = "News from WEYNAK"; // expanded message
		vibrator.vibrate(500);
		// title
		CharSequence contentText = intent.getStringExtra("payload"); // expanded
		// message
		// text

		Intent notificationIntent = new Intent(this, Maps.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		// the next two lines initialize the Notification, using the
		// configurations above
		Notification notification = new Notification(icon, tickerText, when);
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.ledARGB = 0xff00ff00;
		notification.ledOnMS = 300;
		notification.ledOffMS = 1000;
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);
		notificationManager.notify(10001, notification);

		Log.w("C2DMReceiver", "finish");
	}
}
